package com.travel.search.controller;

import com.travel.index.pojo.SearchResult;
import com.travel.index.service.HotelIndexService;
import com.travel.manager.service.HotelService;
import common.pojo.Hotel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class SearchController {

    @Autowired
    private HotelIndexService hotelIndexService;

    @RequestMapping("search.html")
    public ModelAndView search(String keywords, Integer min, Integer max, @RequestParam(defaultValue = "1") Integer page) {

        String queryString = "item_keywords:" + keywords;
        if (StringUtils.isEmpty(keywords)) {//如果不输入条件就全查
            queryString = "*:*";
        }
        //最小价格和最大价格
        if (!StringUtils.isEmpty(min) || !StringUtils.isEmpty(max)) {
            queryString += " AND item_price:[";
            queryString += StringUtils.isEmpty(min) ? "*" : min;
            queryString += " TO ";
            queryString += StringUtils.isEmpty(max) ? "*" : max;
            queryString += "]";
        }
        SearchResult hotels = hotelIndexService.getHotelFromIndex(queryString, page, 6, true);
        return new ModelAndView("hotel_list", "hotels", hotels);

    }

    @Autowired
    private HotelService hotelService;

    @RequestMapping("detail.html")
    public ModelAndView detail(Integer id) {
        Hotel hotel = hotelService.getHotelById(id);
        return new ModelAndView("hotel_detail", "hotel", hotel);
    }
}
